package com.zrkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrkj.entity.TSysRoleResource;
import com.zrkj.entity.vo.Result;
import com.zrkj.mapper.TSysRoleResourceMapper;
import com.zrkj.service.ITSysRoleResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author : 国良
 * @className : ITSysRoleResourceServiceImpl
 * @package: com.ha.service.impl
 * @description : 用户组与资源关联表 服务接口实现
 * @date :  2021-05-14
 */
@Service("iTSysRoleResourceService")
public class ITSysRoleResourceServiceImpl extends ServiceImpl<TSysRoleResourceMapper, TSysRoleResource> implements ITSysRoleResourceService {
    /**
     * 添加
     *
     * @param tSysRoleResource
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(TSysRoleResource tSysRoleResource) {
        return Result.success(this.save(tSysRoleResource));
    }

    /**
     * 更新
     *
     * @param tSysRoleResource
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> update(TSysRoleResource tSysRoleResource) {
        return Result.success(this.updateById(tSysRoleResource));
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> delete(List<Long> idList) {
        return Result.success(this.removeByIds(idList));
    }

    @Transactional(rollbackFor = Exception.class)
    public Result deletebyCon(TSysRoleResource data){
        QueryWrapper<TSysRoleResource> queryWrapper = buildQuery(data);
        this.remove(queryWrapper);
        return Result.success();
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<TSysRoleResource> getById(Long id) {
        return Result.success(baseMapper.selectById(id));
    }

    /**
     * 查询列表
     *
     * @param tSysRoleResource
     * @return
     */
    @Override
    public Result<List<TSysRoleResource>> queryList(TSysRoleResource tSysRoleResource) {
        QueryWrapper<TSysRoleResource> queryWrapper = buildQuery(tSysRoleResource);
        return Result.success(this.list(queryWrapper));
    }

    public Result<IPage<TSysRoleResource>> queryPageList(TSysRoleResource data) {
        QueryWrapper<TSysRoleResource> queryWrapper = buildQuery(data);
        IPage<TSysRoleResource> pageQuery = this.page(data.getPage(), queryWrapper);
        return Result.success(pageQuery);
    }

    private QueryWrapper<TSysRoleResource> buildQuery(TSysRoleResource data){
        QueryWrapper<TSysRoleResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(data.getId()),"`id`",data.getId());
        queryWrapper.eq(StringUtils.isNotBlank(data.getRoleId()),"role_id",data.getRoleId());
        queryWrapper.eq(StringUtils.isNotBlank(data.getResourceId()),"resource_id",data.getResourceId());
        queryWrapper.in(!CollectionUtils.isEmpty(data.getRoleIds()),"role_id",data.getRoleIds());
        return queryWrapper;
    }
}
